package com.lz.job.redis;

import java.util.Set;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.util.StringUtils;

/**
 * Redis配置.
 * <p>
 * 如果工程涉及到redis请执行如下操作:
 * <p>
 * 1.请包 com.lz.job.redis拷贝至src/main/java目录下.<br>
 * 2.在src/main/resource
 * 创建redis.properties加入spring.redis.sentinel.master和spring.redis.sentinel.
 * nodes配置.
 *
 * @author zhaoshb
 * @since 0.0.1
 */
//@Configuration
public class RedisConfiguration {

	@Value("${spring.redis.sentinel.master}")
	private String sentinelMasterName = null;

	@Value("${spring.redis.sentinel.nodes}")
	private String hostAndPorts = null;

	@Bean
	public JedisConnectionFactory connectionFactory() {
		Set<String> sentinelHostAndPorts = StringUtils.commaDelimitedListToSet(this.getHostAndPorts());
		RedisSentinelConfiguration sc = new RedisSentinelConfiguration(this.getSentinelMasterName(), sentinelHostAndPorts);

		return new JedisConnectionFactory(sc);
	}

	private String getSentinelMasterName() {
		return this.sentinelMasterName;
	}

	private String getHostAndPorts() {
		return this.hostAndPorts;
	}

}
